package com.xhj.xmly.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.xhj.xmly.BR;
import com.xhj.xmly.R;
import com.xhj.xmly.adapter.CommentAdapter;
import com.xhj.xmly.bean.CountryRadio;
import com.xhj.xmly.bean.InternetRadio;
import com.xhj.xmly.bean.LocalRadion;
import com.xhj.xmly.bean.ResultBean;
import com.xhj.xmly.callback.HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RadioActivity extends AppCompatActivity implements Callback<CountryRadio>, View.OnClickListener {

    private String url = "http://live.ximalaya.com/";
    private ListView listView;

    private List<ResultBean> coutrylist;
    private CommentAdapter commentAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio);
        TextView textView = (TextView) findViewById(R.id.radio_name);
        listView = (ListView) findViewById(R.id.radio_listview);
        TextView empty = (TextView) findViewById(R.id.empty_tv);
        ImageView imageView = (ImageView) findViewById(R.id.btn_image);
        imageView.setOnClickListener(this);
        listView.setEmptyView(empty);
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        textView.setText(name);
        coutrylist = new ArrayList<>();
        commentAdapter = new CommentAdapter(this, coutrylist, R.layout.broadcast_item2, BR.result);
        listView.setAdapter(commentAdapter);
        HttpService service = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        switch (name){
            case "本地台":
                service.getLocal().enqueue(new Callback<LocalRadion>() {
                    @Override
                    public void onResponse(Call<LocalRadion> call, Response<LocalRadion> response) {
                        List<ResultBean> result = response.body().getResult();
                        commentAdapter.addAll(result);
                    }

                    @Override
                    public void onFailure(Call<LocalRadion> call, Throwable t) {

                    }
                });
                break;
            case "国家台":
                service.getCountry().enqueue(this);
                break;
            case "网络台":
                service.getInternet().enqueue(new Callback<InternetRadio>() {
                    @Override
                    public void onResponse(Call<InternetRadio> call, Response<InternetRadio> response) {
                        List<ResultBean> result = response.body().getResult();
                        commentAdapter.addAll(result);
                    }

                    @Override
                    public void onFailure(Call<InternetRadio> call, Throwable t) {

                    }
                });
                break;
        }

    }

    @Override
    public void onResponse(Call<CountryRadio> call, Response<CountryRadio> response) {
        List<ResultBean> result = response.body().getResult();
        commentAdapter.addAll(result);
    }

    @Override
    public void onFailure(Call<CountryRadio> call, Throwable t) {

    }

    @Override
    public void onClick(View v) {
       finish();
    }
}
